package com.tcm.core.service;


import com.base.core.exception.ServiceException;
import com.base.core.lang.StringUtils;
import com.base.core.utils.JacksonUtils;
import com.tcm.core.mapper.ConfigMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService(version = "0.0.1-alpha", delay = -1, timeout = 6000)
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    private ConfigMapper configMapper;

    @Override
    public String findValue(String key, String defaultValue) {
        String value = configMapper.findByKey(key);
        if(StringUtils.isBlank(value)){
            return defaultValue;
        }
        return value;
    }

    @Override
    public <T> T findConfigObject(String key, Class<T> clazz) {
        String value = findValue(key, null);
        if(StringUtils.isNotBlank(value)){
            return JacksonUtils.toJavaObject(value, clazz);
        }

        try {
            return clazz.newInstance();
        } catch (Exception e) {
            throw new ServiceException("获取参数失败");
        }
    }
}
